﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
using A2DFramework.EventService.DistributeEventBus;
using System.Reflection;
using A2DFramework.XmlService;
using System.Web;
using A2DFramework.AssemblyService;

namespace A2DFramework.CommandBusService.MSMQCommandBus
{
    public class TransferWrapper
    {
        public TransferWrapper()
        {
            ContentSaveLocation = TransferContentSaveLocation.InQueue;
        }

        public string RealCommandType { get; set; }
        public string RealCommandModule { get; set; }
        public string RealCommandXml { get; set; }
        public string NetworkLocation { get; set; }
        public TransferContentSaveLocation ContentSaveLocation { get; set; }

        private const double MaxContentLength=3.5*1024*1024;
        public void InjectCommand(BaseCommand cmd)
        {
            this.RealCommandType = cmd.GetType().FullName;
            this.RealCommandModule = cmd.GetType().Module.Name;
            string xml = SerializeUtils.Serialize2XML(cmd.GetType(), cmd);
            SaveXML(xml);
        }

        public void InjectCommandResult(BaseCommandResult result)
        {
            this.RealCommandType = result.GetType().FullName;
            this.RealCommandModule = result.GetType().Module.Name;
            string xml = SerializeUtils.Serialize2XML(result.GetType(), result);
            SaveXML(xml);
        }
        public BaseCommand ExtractCommand()
        {
            Assembly assembly = AppDomain.CurrentDomain.Load(this.RealCommandModule.TrimEnd(".dll".ToCharArray()));
            Type type = assembly.GetType(this.RealCommandType);


            string xml = GetXML();
            BaseCommand cmd = (BaseCommand)SerializeUtils.DeserializeFromXML(type, xml);
            return cmd;
        }

        
        public BaseCommandResult ExtractCommandResult()
        {
            Assembly assembly = AppDomain.CurrentDomain.Load(this.RealCommandModule.TrimEnd(".dll".ToCharArray()));
            Type type = assembly.GetType(this.RealCommandType);
            string xml = GetXML();
            BaseCommandResult cmd = (BaseCommandResult)SerializeUtils.DeserializeFromXML(type, xml);
            return cmd;
        }



        private string GetXML()
        {
            string xml = "";
            if (this.ContentSaveLocation == TransferContentSaveLocation.InQueue)
                xml = this.RealCommandXml;
            else if (this.ContentSaveLocation == TransferContentSaveLocation.InNetwork)
                xml = NetworkShareFileSaver.GetContent(this.NetworkLocation);
            return xml;
        }
        private void SaveXML(string xml)
        {
            if (xml.Length > MaxContentLength)
            {
                this.NetworkLocation = NetworkShareFileSaver.Save(xml);
                this.ContentSaveLocation = TransferContentSaveLocation.InNetwork;
            }
            else
            {
                this.RealCommandXml = xml;
                this.ContentSaveLocation = TransferContentSaveLocation.InQueue;
            }
        }
    }
}
